**********************************************************
*******  This file creates the files:        (all files in $datadirectory )
*************   "county_pscs_percap_$date$supplytoggle.dta", 
*************   "cd_pscs_percap_$date$supplytoggle.dta", and
*************   "cd_pscs&votes_$date$supplytoggle.dta", 
**************************   
*******  from input files:
*************   county_csps_new_$supplytoggle.dta"  (3137 obs)
*************   county_cd_cw.dta"       (weights for counties to districts)
*************   st_abb_fips.dta"          
*************   joined_county_em_voteview_trun.dta"  (county level data with dwnom & vulcan)
*************   CountyCorn_2002_clean.dta"
*************   CountyCorn_2007_clean.dta"
*************   CountyCorn_2010_clean.dta"
*************   voteview_forck_temp.dta"  (district level dwnom nchoices
*************   vote on WM.dta"            (district level vote and party
*************   LegislatorDollars_010109_123110_clean.dta"  (district level 

clear 
set more off 
est clear
****  if $toggle==1 outsheet and estout
global toggle = 1  
global date = "210726"

****  supply toggles:  $supplytoggle==2 (corrects trapezoids) $supplytoggle==1 (shifts supply out) $supplytoggle==0 (no correction) 
global supplytoggle=2

**** quantile toggles: $numquant = number of quantiles of rfs gains - cat gains
global numquant=4

**** Toggle for Switching Votes of Ag Committee Dem's 0 = actual votes, 1 = switch Dem's to nay's
global PetersonDems = 0

*
*global preamble = "C:\AllStephen\My Dropbox\Jon n Chris"
*global preamble = "/Users/knittel/Dropbox/Landuse"
global preamble = "/Users/Jon/Dropbox/Landuse"
*
global datadirectory = "$preamble/5 - Probits/Data"
global codedirectory = "$preamble/5 - Probits/Code"
global tabledirectory = "$preamble/Write-up/Tables"
**global contribution_ver = "$datadirectory/LegislatorDollars_052609_072609_clean.dta"
**global ver = "3month"
global contribution_ver = "$datadirectory/LegislatorDollars_010109_123110_clean.dta"
global ver = "2year"

*use "$datadirectory/county_csps.dta"
use "$datadirectory/county_csps_new_$supplytoggle.dta", clear
*use "$datadirectory/May 6 Backup/county_csps.dta"

******* create county_pscs_percap
rename dcsps_RFS pscs_rfs
rename dcsps_CT pscs_cat
rename dcsps_LCFS pscs_lcfs
rename dcsps_SUBS pscs_subs

foreach k in subs lcfs rfs cat	{
	gen pscs_`k'_percap = pscs_`k'/population
	}

if $toggle==1{
save "$datadirectory/county_pscs_percap_$date$supplytoggle.dta", replace
}

******* create county_ps_percap
rename dps_RFS ps_rfs
rename dps_CT ps_cat
rename dps_LCFS ps_lcfs
rename dps_SUBS ps_subs

if $toggle==1{
save "$datadirectory/county_pscs_percap_$date$supplytoggle.dta", replace
}

******* begin create cd_pscs_xxx
*use "$datadirectory/county_pscs_percap_$date$supplytoggle.dta", clear
count
sort state county
merge 1:1 state county using "$datadirectory/CountyCorn_2007_clean.dta"
*** Jon:  Why are we getting this extra county?
*** This is Broomfield CO, which was created in 2001 or so and apparently isn't in Nathan's data
drop if _merge==2
rename value corn2007
	
drop _merge
sort state county
merge 1:1 state county using "$datadirectory/CountyCorn_2002_clean.dta"
drop if _merge==2
rename value corn2002	
drop _merge

*drop _merge
*sort state county
*merge 1:1 state county using "$datadirectory/CountyCorn_2010_clean.dta"
*drop if _merge==2
*rename value corn2010
*replace corn2010 = 0 if corn2010 == .
*count
*drop _merge

merge m:1 state using "$datadirectory/st_abb_fips.dta", nogen
merge 1:1 st_abb county using  "$datadirectory/joined_county_em_voteview_trun.dta"
** Check:  why are we getting 4 extra counties in vulcan?
** Broomfield CO is a new county, other 3 are in AK
drop if _merge==2
drop _merge

** Merge county population density and employment share
** Note some county employment share data are missing for counties because of confidentiality reasons
merge 1:1 state county using "$datadirectory/County Employment Share and Population Density.dta"
drop _merge

preserve
****** Create state level dataset
collapse (sum) pscs_subs pscs_lcfs pscs_rfs pscs_cat population corn* vulcan_milton (mean) plant_rate, by (state st_abb)
drop if st_abb=="DC"

foreach k in subs lcfs rfs cat	{
	gen pscs_`k'_percap = pscs_`k'/population
	}
if $toggle==1{
save "$datadirectory/state_pscs_percap_$date$supplytoggle.dta", replace
}
restore
**************

merge 1:m state county using "$datadirectory/county_cd_cw.dta"
drop if _merge==2
** Check:  we're picking up three extra counties, all of which were in vulcan
*** These are in AK and apparently not in Nathan's data set
drop _merge
rename cd110 district

*keep state county population pscs_subs  pscs_lcfs pscs_cat pscs_rfs district county_count weight  corn* commercial industrial residential electricityprod onroad cement aircraft unknown nonroad co2tons 	vulcan_milton  totalmwh plgenacl plgenaol plgenags plgenanc plgenahy othermwh plant_rate cnty_rate vulcan_tonspercap eia_tonspercap eia_milton
*
keep state county population pscs_subs pscs_lcfs pscs_cat pscs_rfs ps_* district county_count weight  corn* commercial industrial residential electricityprod onroad cement aircraft unknown nonroad co2tons 	vulcan_milton  totalmwh plgenacl plgenaol plgenags plgenanc plgenahy othermwh plant_rate cnty_rate vulcan_tonspercap eia_tonspercap eia_milton st_abb emp_* total area_2000 pop_00 popdens_00

foreach k in subs lcfs rfs cat	{
	gen maxpscs_`k'_percap = pscs_`k'/population
	}

xtile rfsquart = maxpscs_rfs_percap, nq(4)
xtile catquart = maxpscs_cat_percap, nq(4)
gen maxrfs_quart= rfsquart==4
gen maxcat_quart= catquart==4

*********** Collapse county data to district data
**
**First create shares*population
**
foreach k in emp_ag_share emp_refin_share emp_auto_share popdens_00 {
replace `k' = `k'*population
}

collapse (sum) pscs_subs pscs_lcfs pscs_rfs pscs_cat ps_* population corn* vulcan_milton emp_ag_share emp_refin_share emp_auto_share popdens_00 (max) max* (mean) plant_rate [iw=weight], by(state st_abb district)

foreach k in subs lcfs rfs cat {
gen pscs_`k'_percap = pscs_`k'/population
}

foreach k in subs lcfs rfs cat {
gen ps_`k'_percap = ps_`k'/population
}

**
** Now divide by population to get weighted measure
**
foreach k in emp_ag_share emp_refin_share emp_auto_share {
replace `k' = `k'/population
}
	
sum plant_rate, detail
gen lplant_rate = ln(plant_rate - r(min) + 1)
*foreach k in commercial industrial residential electricityprod onroad cement aircraft unknown nonroad co2tons 	{
*	gen `k'_percap = `k'/population
*	}
	
if $toggle==1{
save "$datadirectory/cd_pscs_percap_$date$supplytoggle.dta", replace
}

*use "$datadirectory/cd_pscs_percap_$date$supplytoggle.dta", clear
	
merge 1:1 st_abb district using "$datadirectory/vote on WM.dta"
**** we drop two "districts" one is DC the other is CA 32
drop if _merg==1
drop _merge

**** "Cleaner" WM v. RFS test
****** this line will switch the votes of 17 (of 29) ag committee Dems from "Aye" to "Nay"
*****  the idea is that these folks would have voted "Nay" without the Peterson amendment
if $PetersonDems == 1 {
replace vote_cat ="Nay" if ag_com_dem ==1
}

rename state statefip
drop if state==.
sort statefip district
merge 1:1 statefip district using "$datadirectory/voteview_forck_temp.dta"

drop _merge 
drop if st_abb=="."
drop if name_cat==""
merge 1:1 st_abb district using "$contribution_ver"

* New 8/23/11
drop l_amt_supp l_amt_split l_amt_opp
gen l_amt_supp = ln(amt_supp )
gen l_amt_opp = ln(amt_opp )
gen l_amt_split = ln(amt_split )
replace  l_amt_supp = 0 if amt_supp <= 0
replace  l_amt_opp = 0 if amt_opp <= 0
replace  l_amt_split = 0 if amt_spli <= 0

replace amt_opp=amt_opp/1000
replace amt_supp=amt_supp/1000
replace amt_split=amt_split/1000

gen vote_d_cat = 1
replace vote_d_cat = 1 if vote_cat=="Aye"
replace vote_d_cat = 0 if vote_cat=="Nay"

replace vote_d_cat = . if vote_cat=="No Vote"
drop if district ==.

gen dem_cat = party_cat=="D"
replace dem_cat = 1 if name_cat=="Tauscher, Ellen"

foreach k in subs lcfs rfs cat	{
	gen pscs_`k'_DEM = pscs_`k'_percap*dem_cat
	}


foreach k in subs lcfs rfs cat	{
	egen min_pscs_`k'_percap = min(pscs_`k'_percap)
	}

**** Shift PSCS by $100/per cap for all policies, eq. to $100/cap carbon benefit
foreach k in subs lcfs rfs cat	{
	gen lpscs_`k'_percap = ln(pscs_`k'_percap + 100)
	}

gen rfs_larger = pscs_rfs_percap>pscs_cat_percap
gen cat_larger = pscs_rfs_percap<pscs_cat_percap
gen subs_larger = pscs_subs_percap>pscs_cat_percap

**** Difference in CAT/RFS gains in $1000's
gen pscs_rfscat_diff = (pscs_rfs_percap - pscs_cat_percap)/1000
gen pscs_catrfs_diff = (pscs_cat_percap - pscs_rfs_percap)/1000

**** Difference in CAT and RFS gains in logs
gen l_pscs_catrfs_ratio = ln((pscs_cat_percap+100)/(pscs_rfs_percap+100))

xtile pscs_rfscat_diff_quant = pscs_rfscat_diff, nq($numquant)
forvalues i = 1(1)$numquant {
        gen rfscat_diff_quant_`i' = pscs_rfscat_diff_quant==`i'         
	}

xtile pscs_rfs_quart = pscs_rfs_percap, nq(4)
xtile pscs_subs_quart = pscs_subs_percap, nq(4)
xtile pscs_cat_quart = pscs_cat_percap, nq(4)
xtile pscs_rfscat_quart = pscs_rfscat_diff, nq(4)

gen pscs_rfs_quart_1 = pscs_rfs_quart==1
gen pscs_rfs_quart_2 = pscs_rfs_quart==2
gen pscs_rfs_quart_3 = pscs_rfs_quart==3
gen pscs_rfs_quart_4 = pscs_rfs_quart==4

gen pscs_cat_quart_1 = pscs_cat_quart==1
gen pscs_cat_quart_2 = pscs_cat_quart==2
gen pscs_cat_quart_3 = pscs_cat_quart==3
gen pscs_cat_quart_4 = pscs_cat_quart==4

gen pscs_subs_quart_1 = pscs_subs_quart==1
gen pscs_subs_quart_2 = pscs_subs_quart==2
gen pscs_subs_quart_3 = pscs_subs_quart==3
gen pscs_subs_quart_4 = pscs_subs_quart==4

gen pscs_rfscat_quart_1 = pscs_rfscat_quart==1
gen pscs_rfscat_quart_2 = pscs_rfscat_quart==2
gen pscs_rfscat_quart_3 = pscs_rfscat_quart==3
gen pscs_rfscat_quart_4 = pscs_rfscat_quart==4

gen coal_top10=0
replace coal_top=1 if st_abb=="CO"|st_abb=="ND"|st_abb=="VA"|st_abb=="IL"|st_abb=="MT"|st_abb=="TX"|st_abb=="PA"|st_abb=="KY"|st_abb=="WV"|st_abb=="WY"
gen coal_top5=0
replace coal_top5=1 if st_abb=="TX"|st_abb=="PA"|st_abb=="KY"|st_abb=="WV"|st_abb=="WY"

gen l_vulcan = ln(vulcan_milton*1000/population )
gen l_corn_2007 = ln(corn2007+1)
gen lpopdens_00 = ln(popdens_00)

if $PetersonDems == 0 {
if $toggle==1{
save "$datadirectory/cd_pscs&votes_$date$supplytoggle.dta", replace
}
}

if $PetersonDems == 1 {
if $toggle==1{
save "$datadirectory/cd_pscs&votes_$date$supplytoggle_PeteDems.dta", replace
}
}

drop if st_abb=="DC"

*use "$datadirectory/county_csps.dta"
use "$datadirectory/county_csps_new_325_$supplytoggle.dta"
*use "$datadirectory/May 6 Backup/county_csps.dta"

******* create county_pscs_percap
rename dcsps_RFS pscs_rfs
rename dcsps_CT pscs_cat
rename dcsps_LCFS pscs_lcfs
rename dcsps_SUBS pscs_subs

foreach k in subs lcfs rfs cat	{
	gen pscs_`k'_percap = pscs_`k'/population
	}

if $toggle==1{
save "$datadirectory/county_pscs_percap_325_$date$supplytoggle.dta", replace
}

